/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanManageUserExpressions.java
 *
 * Created on 22-ago-2011, 12:12:18
 */

package br.usp.ime.faguilar.guis;

import br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanWriting;
import DatabaseMathExpressions.DBFuntions;
import DatabaseMathExpressions.UserExpression;
import br.usp.ime.faguilar.guis.capturers.PanInputUserMathExpression;
import br.usp.ime.faguilar.guis.Util.MyTableModel;
import MathExpression.Graphics.GMathExpression;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author frank
 */
public class PanManageUserExpressions extends javax.swing.JPanel
implements ListSelectionListener{

    /** Creates new form PanManageUserExpressions */
    private DBFuntions dbFunctions;
    private String myUser;
    private String[] columnNames={"ID-Expression","Model","Time of input"};
    private UserExpression selectedUExpression;
    private JPanel panUpdateAdvance;

    public PanManageUserExpressions() {
        initComponents();
        initMyAttributes();
    }

    private void initMyAttributes(){
        dbFunctions=null;
        myUser="";
        selectedUExpression=null;
        panUpdateAdvance=null;
        this.jTableData.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ListSelectionModel rowSM = jTableData.getSelectionModel();
        rowSM.addListSelectionListener(this);
    }

    public JPanel getPanUpdateAdvance() {
        return panUpdateAdvance;
    }

    public void setPanUpdateAdvance(JPanel panUpdateAdvance) {
        this.panUpdateAdvance = panUpdateAdvance;
    }

    public void chargeMyUserExpressions(){
        ArrayList<UserExpression> expressionsForUser = dbFunctions.getExpressionsForUser(myUser);
        Object[][] data=preprocessMyDataForTable(expressionsForUser);
        MyTableModel mtm=new MyTableModel(columnNames,data);
        this.jTableData.setModel(mtm);
        jTableData.repaint();
    }

    public String getMyUser() {
        return myUser;
    }

    public void setMyUser(String myUser) {
        this.myUser = myUser;
    }

    private Object[][] preprocessMyDataForTable(ArrayList<UserExpression> expressionsForUser){
        Object[][] data=new Object[expressionsForUser.size()][columnNames.length];
        String idExpression;
        Timestamp tm=null;
        Integer idModel;
        int i=0;
        for (UserExpression ue : expressionsForUser) {
            idExpression=DBFuntions.codeIdUserExpression(ue.getIdUser(),ue.getId());
            tm=ue.getTimeStampInput();
            idModel=ue.getIdModelExpression();
            Object[] obj={idExpression,idModel,tm.toString()};
            data[i]=obj;
            i++;
        }
        return data;
    }

    public DBFuntions getDbFunctions() {
        return dbFunctions;
    }

    public void setDbFunctions(DBFuntions dbFunctions) {
        this.dbFunctions = dbFunctions;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        panShowMathExpression = new br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanNotEditableNamedMathExpression();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableData = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 400));
        jPanel1.setLayout(new java.awt.BorderLayout());

        org.jdesktop.layout.GroupLayout panShowMathExpressionLayout = new org.jdesktop.layout.GroupLayout(panShowMathExpression);
        panShowMathExpression.setLayout(panShowMathExpressionLayout);
        panShowMathExpressionLayout.setHorizontalGroup(
            panShowMathExpressionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 504, Short.MAX_VALUE)
        );
        panShowMathExpressionLayout.setVerticalGroup(
            panShowMathExpressionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 380, Short.MAX_VALUE)
        );

        jPanel1.add(panShowMathExpression, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("My expressions"));
        jPanel2.setPreferredSize(new java.awt.Dimension(350, 400));

        jButton1.setText("Delete");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jTableData);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton1))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .add(10, 10, 10)
                .add(jButton1))
        );

        add(jPanel2, java.awt.BorderLayout.WEST);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        if(selectedUExpression!=null){
            dbFunctions.deleteUserExpressionByIDUserAndIndex(myUser,selectedUExpression.getId());
            this.panShowMathExpression.clearPanel();
            this.chargeMyUserExpressions();
            ((PanInputUserMathExpression)panUpdateAdvance).upDateAdvance();
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableData;
    private br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanNotEditableNamedMathExpression panShowMathExpression;
    // End of variables declaration//GEN-END:variables

    public void valueChanged(ListSelectionEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
        //Ignore extra messages.
                    if (e.getValueIsAdjusting()) return;

                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    if (lsm.isSelectionEmpty()) {
                        System.out.println("No rows are selected.");
                    } else {
                        int selectedRow = lsm.getMinSelectionIndex();
                        Object value=this.jTableData.getModel().getValueAt(selectedRow,0);
                        String[] idUserAndIndex=DBFuntions.decodeIDUserExpression((String)value);
                        selectedUExpression=dbFunctions.getUserExpression(idUserAndIndex[0], Integer.valueOf(idUserAndIndex[1]));
                        ((GMathExpression) selectedUExpression.getdMExpression()).showJustMathExpression();
                        panShowMathExpression.setDrawable((GMathExpression) selectedUExpression.getdMExpression());
                        panShowMathExpression.repaint();
//                        System.out.println("Row " + selectedRow
//                                           + " is now selected.");

                    }
    }

}
